package com.eliza.springboot.v3.http.control.webFlux

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.MediaType
import org.springframework.stereotype.Component
import org.springframework.web.servlet.function.*

@Component
class MyUserHandler {
    fun getUser(serverRequest: ServerRequest): ServerResponse {
        return ServerResponse.ok().body(mapOf("as" to "ds"))
    }

    fun deleteUser(serverRequest: ServerRequest): ServerResponse {
        return ServerResponse.ok().build()

    }

}


//函数式 web

@Configuration(proxyBeanMethods = false)
class MyRoutingConfiguration {

    @Bean // Bean 方法参数是对像 自动注入
    fun routerFunction(userHandler: MyUserHandler): RouterFunction<ServerResponse> {
        return RouterFunctions.route()
            .POST("/api/func/{id}", ACCEPT_JSON, userHandler::getUser)
            .DELETE("/api/func/{id}", ACCEPT_JSON, userHandler::deleteUser)
            .build()
    }


    companion object {
        //        请求方式
        private val ACCEPT_JSON = RequestPredicates.accept(MediaType.APPLICATION_JSON)
    }

}